home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / bprof-1.1 / bprof-1 / bprof / sources.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-31  |  2.6 KB  |  143 lines

  1. #include <fstream.h>
  2. #include <sys/stat.h>
  3. #include "sources.h"
  4.  
  5. sourcefile::sourcefile(String name, int really)
  6. {
  7.     filename = name;
  8.  
  9.     if (!really) {
  10.     lines = 0;
  11.     return;
  12.     }
  13.     
  14.     ifstream from(name);
  15.     if (!from) {
  16.     lines = 0;
  17.     return;
  18.     }
  19.  
  20.     /* Count the number of lines in the sourcefile.
  21.        Numlines is actually one larger than this. */
  22.     numlines = 1;
  23.     char* ch;
  24.     while (from.gets(&ch)) {
  25.     delete[] ch;
  26.     numlines++;
  27.     }
  28.  
  29.     lines = new int[numlines];
  30.     memset(lines, -1, numlines * sizeof(int));
  31.     /* The above line assumes that this will make -1 out
  32.        of them */
  33.  
  34.     stat flstat;
  35.     if (stat(name, &flstat)) {
  36.     cerr << "Warning: could not stat " << name << '\n';
  37.         _mtime = 0;
  38.     return;
  39.     }
  40.     _mtime = flstat.st_mtime;
  41. }
  42.  
  43. sourcefile::~sourcefile(void)
  44. {
  45.     delete[] lines;
  46. }
  47.  
  48. int& sourcefile::operator[](unsigned int nr)
  49. {
  50.     if (!lines)
  51.     return dummy;
  52.  
  53.     if (nr >= numlines) {
  54.     cerr << "Warning: debug info indicates line " << nr << " in file " << filename
  55.         << "\nwhich seems to have only " << (numlines-1) << " lines\n";
  56.     return dummy;
  57.     }
  58.  
  59.     if (lines[nr] == -1)
  60.     lines[nr] = 0;
  61.     return lines[nr];
  62. }
  63.  
  64. void sourcefile::paste(const char *suffix)
  65. {
  66.     if (!lines)
  67.     return;
  68.  
  69.     // Should check length with pathconf(".", _PC_NAME_MAX)
  70.     String outname(filename + suffix);
  71.     unlink(outname);        // opening with ios::trunc doesn't seem to work ???
  72.     ofstream to(outname);
  73.     if (!to) {
  74.     cerr << "Could not open " << outname << " to write to\n";
  75.     return;
  76.     }
  77.     
  78.     ifstream from(filename);
  79.     if (!from) {
  80.     cerr << "Could not open " << filename << " for input\n";
  81.     return;
  82.     }
  83.  
  84.     for (int i = 1; i < numlines; i++) {
  85.     if (lines[i] == -1)
  86.         to << "     .";
  87.     else if (lines[i] == 0)
  88.         to << "     -";
  89.     else {
  90.         to.width(6);
  91.         to << lines[i];
  92.     }
  93.     char* ch;
  94.     from.gets(&ch);
  95.     to << '\t' << ch << '\n';
  96.     delete[] ch;
  97.     }
  98. }
  99.  
  100. time_t sourcefile::mtime(void) const
  101. {
  102.     return lines ? _mtime : 0;
  103. }
  104.  
  105. dirset::dirset(void)
  106. {
  107.     first = 0;
  108. }
  109.  
  110. dirset::~dirset(void)
  111. {
  112.     dirlink* nxt;
  113.     for (dirlink* i = first; i; i = nxt) {
  114.     nxt = i->next;
  115.     delete i;
  116.     }
  117. }
  118.  
  119. void dirset::operator+=(const char *name)
  120. {
  121.     stat dirstat;
  122.     if (stat(name, &dirstat))
  123.     return;
  124.     dirlink* newdir = new dirlink;
  125.     newdir->dev = dirstat.st_dev;
  126.     newdir->ino = dirstat.st_ino;
  127.     newdir->next = first;
  128.     first = newdir;
  129. }
  130.  
  131. int dirset::contains(const char *name)
  132. {
  133.     stat dirstat;
  134.     if (stat(name, &dirstat))
  135.     return 0;
  136.     for (dirlink* i = first; i; i = i->next) {
  137.     if (dirstat.st_dev == i->dev &&
  138.         dirstat.st_ino == i->ino)
  139.         return 1;
  140.     }
  141.     return 0;
  142. }
  143.